//
//  ZYViewController.m
//  NewPro
//
//  Created by RYX on 2019/4/28.
//  Copyright © 2019 何荧红. All rights reserved.
//

#import "ZYViewController.h"
#import "KnowledgeStore.h"
#import "KnowledgeModel.h"
#import "KnowledgeCell.h"
#import "KnowledgeList.h"

@interface ZYViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *listArr;

@end

@implementation ZYViewController

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = 0.5;
        _tableView.sectionHeaderHeight = 9.5;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
        
        MJWeakSelf
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [weakSelf refresh];
        }];
        
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;
        
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(0);
            
            make.bottom.equalTo(weakSelf.view).offset(-45);
            
            
        }];
        
    }
    
    return _tableView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)refresh{
    
    MJWeakSelf
    KnowledgeStore *store = [[KnowledgeStore alloc] init];
    [store getListWithType:@"2" Success:^(NSArray *listArr) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        
        weakSelf.listArr = [NSMutableArray arrayWithArray:listArr];
        
        [weakSelf.tableView reloadData];
        
    } Failure:^(NSError *error) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
    
}



#pragma mark - UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.listArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    KnowledgeModel *model = self.listArr[indexPath.section];
    KnowledgeCell *cell = [KnowledgeCell tempWithTableView:tableView];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [KnowledgeList sharedInstance].currentModel = self.listArr[indexPath.section];
    if (self.delegate && [self.delegate respondsToSelector:@selector(onCell)]) {
        
        [self.delegate onCell];
    }
    
}
@end
